package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    //文件上传
    @Override
    public ResponseResult uploadFile(MultipartFile multipartFile) {

//        检验参数
        if (multipartFile == null || multipartFile.getSize() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        String fileName = multipartFile.getOriginalFilename();

        String sub = fileName.substring(fileName.lastIndexOf("."));

        String prex = UUID.randomUUID().toString().replace("-", "");

        fileName = sub + prex;


//        文件上传

        try {
            String url = fileStorageService.uploadImgFile("", fileName, multipartFile.getInputStream());


            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setIsCollection((short) 0);
            wmMaterial.setUrl(url);
            wmMaterial.setType((short) 0);
            wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
            wmMaterial.setCreatedTime(new Date());
            baseMapper.insert(wmMaterial);
            return ResponseResult.okResult(wmMaterial);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public ResponseResult findListImages(WmMaterialDto dto) {

//        判断分页
        dto.checkParam();

        Page<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());

        QueryWrapper<WmMaterial> wrapper = new QueryWrapper<WmMaterial>();

//        判断是否收藏
        if (dto.getIsCollection() != null && dto.getIsCollection() == 1) {
//                查询收藏
            wrapper.eq("is_collection", dto.getIsCollection());
        } else {
            wrapper.eq("is_collection", dto.getIsCollection());
        }
        wrapper.eq("user_id", WmThreadLocalUtil.getUser().getId());

        wrapper.orderByDesc("created_time");

        Page<WmMaterial> wmMaterialPage = baseMapper.selectPage(page,wrapper);

        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) wmMaterialPage.getTotal());

        responseResult.setData(wmMaterialPage.getRecords());

        return responseResult;
    }


}
